/*
 * @Descripttion:
 * @version:
 * @Author: wukunling
 * @Date: 2021-09-02 21:04:32
 * @LastEditors: sueRimn
 * @LastEditTime: 2021-09-02 21:40:32
 */
"use strict";

// const Controller = require('egg').Controller;
const fs = require("fs");
const path = require("path");
//故名思意 异步二进制 写入流
const awaitWriteStream = require("await-stream-ready").write;
//管道读入一个虫洞。
const sendToWormhole = require("stream-wormhole");
const dayjs = require("dayjs");
const Controller = require("./baseController");
/**
 * @controller ToolController 上文件接口
 */
class ToolController extends Controller {
  /**
   * @summary 文件上传
   * @description formData 案例
   * @router post /v1/tool/upload
   * @request formData file *file
   * @response 200 JsonResult 操作结果
   */
  async upload() {
    const stream = await this.ctx.getFileStream();
    // 基础的目录
    const uploadBasePath = "app/public/uploads";
    // 生成文件名
    const filename = `${Date.now()}${Number.parseInt(
      Math.random() * 1000
    )}${path.extname(stream.filename).toLocaleLowerCase()}`;
    // 生成文件夹
    const dirname = dayjs(Date.now()).format("YYYY/MM/DD");
    function mkdirsSync(dirname) {
      if (fs.existsSync(dirname)) {
        return true;
      } else {
        if (mkdirsSync(path.dirname(dirname))) {
          fs.mkdirSync(dirname);
          return true;
        }
      }
    }
    mkdirsSync(path.join(uploadBasePath, dirname));
    // 生成写入路径
    const target = path.join(uploadBasePath, dirname, filename);
    // 写入流
    const writeStream = fs.createWriteStream(target);
    try {
      //异步把文件流 写入
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      //如果出现错误，关闭管道
      await sendToWormhole(stream);
      this.ctx.throw(500, err);
    }
    const { protocol, host } = this.ctx.request;
    let url = path
      .join("/public/uploads", dirname, filename)
      .replace(/\\|\//g, "/");
    url = protocol + "://" + host + url;
    this.jsonBody(url);
  }
}

module.exports = ToolController;
